/**
 * Copyright (C) 2010 - 2013 Harry Glasgow
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package com.googlecode.jaden.ui.dialog.projectdefinition;

import com.googlecode.jaden.common.config.IOConfiguration;
import com.googlecode.jaden.common.enums.DateResultType;
import com.googlecode.jaden.common.other.Utils;
import static com.googlecode.jaden.ui.Constants.*;
import com.googlecode.jaden.ui.UiUtils;
import com.googlecode.jaden.ui.dialog.BaseDialog;

import javax.swing.*;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.prefs.Preferences;

public class EditDateIODialog extends BaseDialog {

    private IOConfiguration configuration;
    private JTextField formatTextField;
    private JLabel exampleLabel;
    private JComboBox<DateResultType> resultComboBox;

    public EditDateIODialog(EditProjectDefinitionDialog dialog, IOConfiguration configuration,
                            Preferences preferences) {
        super(dialog, DIALOG_PROJECT_DETAILS_DATE_TITLE);
        this.configuration = configuration;

        formatTextField = new JTextField(20);

        exampleLabel = new JLabel();

        DefaultComboBoxModel<DateResultType> resultComboModel = new DefaultComboBoxModel<DateResultType>();
        resultComboBox = new JComboBox<DateResultType>(resultComboModel);

        JLabel formatTextLabel = new JLabel(Utils.translate(LABEL_FORMAT_TEXT));
        JLabel exampleTextLabel = new JLabel(Utils.translate(LABEL_EXAMPLE_TEXT));
        JLabel resultTextLabel = new JLabel(Utils.translate(LABEL_RESULT_TEXT));

        JPanel mainPanel = UiUtils.verticalList(
                UiUtils.horizontalList(formatTextLabel, formatTextField),
                UiUtils.horizontalList(exampleTextLabel, exampleLabel),
                UiUtils.horizontalList(resultTextLabel, resultComboBox));
        setMainPanel(mainPanel, preferences);

        UiUtils.enWidth(formatTextLabel,  exampleTextLabel,  resultTextLabel);

        formatTextField.addKeyListener(new KeyListener() {
            public void keyTyped(KeyEvent e) {
                redoExample();
            }

            public void keyPressed(KeyEvent e) {
                redoExample();
            }

            public void keyReleased(KeyEvent e) {
                redoExample();
            }
        });

        resultComboModel.addElement(DateResultType.MonthOfYear);
        resultComboModel.addElement(DateResultType.DayOfWeek);
        resultComboModel.addElement(DateResultType.DayOfMonth);
        resultComboModel.addElement(DateResultType.DayOfYear);
        resultComboModel.addElement(DateResultType.HourOfDay);
        resultComboModel.addElement(DateResultType.MinuteOfHour);
        resultComboModel.addElement(DateResultType.SecondOfMinute);
        resultComboModel.addElement(DateResultType.DayOfWeekCategorical);
        resultComboModel.addElement(DateResultType.MonthCategorical);

        formatTextField.setText(configuration.getDateFormat());
        resultComboBox.setSelectedItem(configuration.getDateResultType());
        redoExample();
    }

    private void redoExample() {
        try {
            SimpleDateFormat sdf = new SimpleDateFormat(formatTextField.getText());
            String text = sdf.format(new Date());
            exampleLabel.setText(text);
        } catch (Exception e) {
            // Don't care.
        }
    }

    protected void doOk() {
        dispose();
        configuration.setDateFormat(formatTextField.getText());
        configuration.setDateResultType((DateResultType) resultComboBox.getSelectedItem());
    }

}